package hiring.tong.com.company.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import hiring.tong.com.common.Constant;
import hiring.tong.com.common.PageModel;
import hiring.tong.com.company.dao.ApplicationDao;
import hiring.tong.com.company.dao.PositionDao;
import hiring.tong.com.company.entity.ApplicationEntity;
import hiring.tong.com.company.entity.CompanyEntity;
import hiring.tong.com.company.entity.PositionEntity;
import hiring.tong.com.talent.dao.ResumeDao;
import hiring.tong.com.talent.entity.ResumeEntity;
import hiring.tong.com.talent.entity.TalentEntity;

/**
 * Servlet implementation class PositionServlet
 */
public class PositionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       private PositionDao positionDao=new PositionDao();
       private ApplicationDao applicationDao=new ApplicationDao();
       private ResumeDao resumeDao=new ResumeDao();
       
  

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String command=request.getParameter("command");
		if("list".equals(command)){
			list(request,response);
		}
		else if("get".equals(command)){
			get(request,response);
		}
		else if("gets".equals(command)){
			gets(request,response);
		}
		else if("add".equals(command)){
			add(request,response);
		}
		else if("lists".equals(command)){
			lists(request,response);
		}
		else if("preupdate".equals(command)){
			preupdate(request,response);
		}
		else if("delete".equals(command)){
			delete(request,response);
		}
		else if("update".equals(command)){
			update(request,response);
		}
		else {
			throw new RuntimeException("非法操作");
		}
	}

	private void gets(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int id=Integer.parseInt(request.getParameter("id"));
		
		PositionEntity position=positionDao.gets(id);
		request.setAttribute("position", position);
		request.getRequestDispatcher("view/company/position-update.jsp").forward(request, response);
	}

	private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int id=Integer.parseInt(request.getParameter("id"));
		String name=request.getParameter("name");
		String duty=request.getParameter("duty");
		String ability=request.getParameter("ability");
		int salary1=Integer.parseInt(request.getParameter("salary1"));
		int salary2=Integer.parseInt(request.getParameter("salary2"));
		PositionEntity position=new PositionEntity();
		position.setId(id);
		position.setName(name);
		position.setDuty(duty);
		position.setAbility(ability);
		position.setSalary1(salary1);
		position.setSalary2(salary2);
		int row =positionDao.update(position);
		request.getRequestDispatcher("PositionServlet?command=preupdate&rows="+row).forward(request, response);
		
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		int id=Integer.parseInt(request.getParameter("id"));
	
		int row=positionDao.delete(id);
		response.sendRedirect("PositionServlet?command=preupdate&row="+row);

		
	}

	private void preupdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int row =0;
		String rowStr=request.getParameter("row");
		String rowStrs=request.getParameter("rows");
		if(rowStr!=null){
			row=Integer.parseInt(rowStr);
			if(row==1){
				request.setAttribute("mession", "删除成功");	
			}
			else{
				request.setAttribute("mession", "删除失败");	
			}
		}
		if(rowStrs!=null){
			row=Integer.parseInt(rowStrs);
			if(row==1){
				request.setAttribute("mession", "修改成功");	
			}
			else{
				request.setAttribute("mession", "修改失败");	
			}
		}
		CompanyEntity company=(CompanyEntity)request.getSession().getAttribute("company");
		int company_id=company.getId();
		
		int pageNo=Constant.DEFAULT_PAGE_NO;
		String pageStr=request.getParameter("pageNo");
		if(pageStr!=null){
			pageNo=Integer.parseInt(pageStr);
		}
		PositionEntity position=new PositionEntity();
		position.setCompanyId(company_id);
		PageModel<PositionEntity> pagemodel=positionDao.list(position,pageNo,Constant.DEFAULT_PAGE_SIZE);
		request.setAttribute("pagemodel", pagemodel);
		request.getRequestDispatcher("view/company/position-delete.jsp").forward(request, response);
		
		
		
		
		
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		CompanyEntity company=(CompanyEntity)request.getSession().getAttribute("company");
		int company_id=company.getId();
		String name=request.getParameter("name");
		String duty=request.getParameter("duty");
		String ability=request.getParameter("ability");
		int salary1=Integer.parseInt(request.getParameter("salary1"));
		int salary2=Integer.parseInt(request.getParameter("salary2"));
		PositionEntity position=new PositionEntity();
		position.setCompanyId(company_id);
		position.setName(name);
		position.setDuty(duty);
		position.setAbility(ability);
		position.setSalary1(salary1);
		position.setSalary2(salary2);
		int row=positionDao.add(position);
		
		if(row==1){
			request.setAttribute("row", row);
			request.getRequestDispatcher("view/company/position-add.jsp").forward(request, response);
		}
		else{
			request.setAttribute("mession", "发布失败");
			request.getRequestDispatcher("view/company/position-add.jsp").forward(request, response);
		}
		
	}

	private void lists(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		CompanyEntity company=(CompanyEntity)request.getSession().getAttribute("company");
		int company_id=company.getId();
		
		int pageNo=Constant.DEFAULT_PAGE_NO;
		String pageStr=request.getParameter("pageNo");
		if(pageStr!=null){
			pageNo=Integer.parseInt(pageStr);
		}
		PositionEntity position=new PositionEntity();
		position.setCompanyId(company_id);
		if(request.getParameter("salary")!=null){
			int salary=Integer.parseInt(request.getParameter("salary"));
			System.out.println(salary);
			position.setSalary(salary);	
		}
		PageModel<PositionEntity> pagemodel=positionDao.list(position,pageNo,Constant.DEFAULT_PAGE_SIZE);
		
		request.setAttribute("pagemodel", pagemodel);
		request.setAttribute("position", position);
		request.getRequestDispatcher("view/company/position-query.jsp").forward(request,response);
		
	}

	private void get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		TalentEntity talent=(TalentEntity)request.getSession().getAttribute("talent");
		if(talent==null){
			request.getRequestDispatcher("view/talent/talent-login.jsp").forward(request,response);
		}
		else{
			int id=Integer.parseInt(request.getParameter("id"));
			
			PositionEntity position=positionDao.get(id);
			request.setAttribute("position", position);
			
			int talent_id=talent.getId();
			ApplicationEntity application=applicationDao.unknowGet(id,talent_id);
			List<ResumeEntity> resumeList=resumeDao.list(talent_id);
			request.setAttribute("application", application);
			request.setAttribute("resumeList", resumeList);
			request.getRequestDispatcher("view/company/position-deliver.jsp").forward(request,response);
			
			
		}
		
	}

	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	
		List<PositionEntity> positionList=positionDao.list();
		request.setAttribute("mession", "职位查询成功");
		request.setAttribute("positionList", positionList);
		request.getRequestDispatcher("index.jsp").forward(request, response);
	}

}
